package com.yanqu.road.dao.impl.activity.xingyoushanshui.cross;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;
import com.yanqu.road.dao.db.DbNameStrategy;
import com.yanqu.road.dao.db.DbParameter;
import com.yanqu.road.dao.impl.TempDataObjectDao;
import com.yanqu.road.entity.activity.youxing.CrossXingYouShanShuiData;
import com.yanqu.road.entity.activity.youxing.ShanShuiBeauty;

import java.sql.CallableStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Types;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class CrossXingYouShanShuiDataDaoImpl extends TempDataObjectDao<CrossXingYouShanShuiData> {

    public static String getCrossXingYouShanShuiDataSql = "select * from `t_c_xing_you_data` where `activityId`=? and groupId = ?";
    public CrossXingYouShanShuiData getCrossXingYouShanShuiData(int activityId, int groupId) {
        ResultSet rs = null;
        CallableStatement call = null;
        try {
            getDb().setConnName(DbNameStrategy.getCrossDb());
            call = getDb().getConnection().prepareCall(getCrossXingYouShanShuiDataSql);
            int i = 1;
            call.setInt(i++, activityId);
            call.setInt(i++, groupId);
            rs = call.executeQuery();
            while (rs.next()) {
                return parse(rs);
            }
        } catch (Exception e) {
            throw new RuntimeException(e);} finally {
            getDb().closeConnection(call, rs);
        }
        return null;
    }

    public static String updateCrossXingYouShanShuiDataSql = "update `t_c_xing_you_data` set userId=?,totalHp=?,hp=?,beautyList=?,maZuChangeTime=?,dayRestTime=? where activityId=? and groupId = ?";
    @Override
    protected boolean updateData(CrossXingYouShanShuiData crossXingYouShanShuiData) {
        crossXingYouShanShuiData.setBeginWriteTime();
        getDb().setConnName(DbNameStrategy.getCrossDb());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<Integer, DbParameter>();
        para.put(i++, new DbParameter(Types.BIGINT, crossXingYouShanShuiData.getUserId()));
        para.put(i++, new DbParameter(Types.BIGINT, crossXingYouShanShuiData.getTotalHp()));
        para.put(i++, new DbParameter(Types.BIGINT, crossXingYouShanShuiData.getHp()));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(crossXingYouShanShuiData.getBeautyList())));
        para.put(i++, new DbParameter(Types.BIGINT, crossXingYouShanShuiData.getMaZuChangeTime()));
        para.put(i++, new DbParameter(Types.BIGINT, crossXingYouShanShuiData.getDayRestTime()));
        para.put(i++, new DbParameter(Types.INTEGER, crossXingYouShanShuiData.getActivityId()));
        para.put(i++, new DbParameter(Types.INTEGER, crossXingYouShanShuiData.getGroupId()));
        if (getDb().executeNoneQuery(updateCrossXingYouShanShuiDataSql, para) > 0) {
            return true;
        }
        return false;
    }

    public static String insertCrossXingYouShanShuiDataSql = "insert into `t_c_xing_you_data`(activityId,groupId,userId,totalHp,hp,beautyList,maZuChangeTime,dayRestTime) values (?,?,?,?,?,?,?,?)";
    @Override
    protected boolean addData(CrossXingYouShanShuiData crossXingYouShanShuiData) {
        crossXingYouShanShuiData.setBeginWriteTime();
        getDb().setConnName(DbNameStrategy.getCrossDb());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<Integer, DbParameter>();
        para.put(i++, new DbParameter(Types.INTEGER, crossXingYouShanShuiData.getActivityId()));
        para.put(i++, new DbParameter(Types.INTEGER, crossXingYouShanShuiData.getGroupId()));
        para.put(i++, new DbParameter(Types.BIGINT, crossXingYouShanShuiData.getUserId()));
        para.put(i++, new DbParameter(Types.BIGINT, crossXingYouShanShuiData.getTotalHp()));
        para.put(i++, new DbParameter(Types.BIGINT, crossXingYouShanShuiData.getHp()));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(crossXingYouShanShuiData.getBeautyList())));
        para.put(i++, new DbParameter(Types.BIGINT, crossXingYouShanShuiData.getMaZuChangeTime()));
        para.put(i++, new DbParameter(Types.BIGINT, crossXingYouShanShuiData.getDayRestTime()));
        if (getDb().executeNoneQuery(insertCrossXingYouShanShuiDataSql, para) > 0) {
            return true;
        }
        return false;

    }

    @Override
    protected CrossXingYouShanShuiData parseData(ResultSet rs) throws SQLException {
        CrossXingYouShanShuiData crossXingYouShanShuiData = new CrossXingYouShanShuiData();
        crossXingYouShanShuiData.setActivityId(rs.getInt("activityId"));
        crossXingYouShanShuiData.setUserId(rs.getLong("userId"));
        crossXingYouShanShuiData.setTotalHp(rs.getLong("totalHp"));
        crossXingYouShanShuiData.setHp(rs.getLong("hp"));
        crossXingYouShanShuiData.setBeautyList(JSON.parseObject(rs.getString("beautyList"), new TypeReference<List<ShanShuiBeauty>>() {}));
        crossXingYouShanShuiData.setMaZuChangeTime(rs.getLong("maZuChangeTime"));
        crossXingYouShanShuiData.setDayRestTime(rs.getLong("dayRestTime"));
        crossXingYouShanShuiData.setGroupId(rs.getInt("groupId"));
        return crossXingYouShanShuiData;
    }
}
